package com.wuage.interfaces.permission;

import com.wuage.application.IUacPermissionService;
import com.wuage.application.IUacRolePermissionService;
import com.wuage.domain.model.UacPermission;
import com.wuage.domain.model.UacRolePermission;
import com.wuage.infrastructure.utils.BeanUtils;
import com.wuage.infrastructure.utils.JwtUtils;
import com.wuage.infrastructure.utils.Response;
import com.wuage.interfaces.permission.domain.RolePermissionUpdateDO;
import com.wuage.interfaces.permission.vo.RolePermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Shynin
 * @version 1.0
 * @date 9/29/21 1:45 PM
 */
@RestController
@Api(tags = "{角色权限}-相关接口")
@RequestMapping("/rolePermission")
public class RolePermissionController {

    @Resource
    private IUacRolePermissionService uacRolePermissionService;

    @Resource
    private IUacPermissionService uacPermissionService;

    @GetMapping("/{roleId}")
    @ApiOperation("查询某个角色关联的权限")
    public Response<List<RolePermissionVO>> get(@ApiParam("角色id") @PathVariable("roleId") Long roleId) {
        //注：当前用户只能查询出自己所拥有的全部权限
        //查询登录用户所拥有权限
        List<UacPermission> loginUserPermission = uacPermissionService.getPermissionByRole(JwtUtils.getLoginUser().getRole());
        //查询角色拥有的权限
        List<UacRolePermission> rolePermissions = uacRolePermissionService.selectPermission(roleId);
        //判断roleId这个角色拥有哪些权限
        List<RolePermissionVO> rolePermissionVOS = BeanUtils.copyCollections(loginUserPermission, RolePermissionVO.class);
        checkPermission(rolePermissionVOS, rolePermissions);

        return Response.success(rolePermissionVOS);
    }

    @PutMapping("/")
    @ApiOperation("更新某个角色所拥有的权限")
    public Response update(@RequestBody @Valid List<RolePermissionUpdateDO> rolePermissionUpdateDOS) {
        Boolean result = uacRolePermissionService.updateRolePermission(rolePermissionUpdateDOS);
        return Response.success(result);
    }

    /**
     * 判断某个角色拥有哪些权限
     * @param rolePermissionVOS 所有的权限
     * @param rolePermissions 某个角色锁拥有的权限
     */
    private void checkPermission(List<RolePermissionVO> rolePermissionVOS, List<UacRolePermission> rolePermissions) {
        if(!CollectionUtils.isEmpty(rolePermissionVOS) && !CollectionUtils.isEmpty(rolePermissions)) {
            //角色所拥有的所有权限
            List<Long> permissionIds = rolePermissions.stream().map(UacRolePermission::getMenuId).collect(Collectors.toList());
            for(RolePermissionVO rolePermissionVO : rolePermissionVOS) {
                //判断这个角色是否有这个权限
                if(permissionIds.contains(rolePermissionVO.getId())) {
                    rolePermissionVO.setHasPermission(true);
                }
                //子权限递归遍历
                if(!CollectionUtils.isEmpty(rolePermissionVO.getChildPermission())) {
                    checkPermission(rolePermissionVO.getChildPermission(), rolePermissions);
                }
            }
        }
    }
}
